<?php
/**
 * Created by PhpStorm.
 * User: 牛赞赞
 * Date: 2020/12/23
 * Time: 18:18
 */

namespace App\Model;


class User extends Model
{
    protected $table = 'user';
    protected $fillable = ['id', 'experice', 'group_id', 'username', 'nickname', 'avatar', 'score'];
    public $timestamps = false;

    public static function userInfo($uid)
    {
        try {
            return self::query()->where('id', $uid)->select('id as account', 'nickname', 'score', 'avatar')->first()->toArray();
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function getUserInfoByUsername($username)
    {
        try {
            return self::query()->where('username', $username)->select('id as account', 'nickname',
                'score', 'avatar')->first()->toArray();
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function getUsers($uidArr)
    {
        try {
            $con = self::query()->select('id as account', 'nickname', 'score', 'avatar')->find($uidArr)->toArray();
            return ['code' => 0, 'data' => $con];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function updateScore($account, $score)
    {
        try {
            // 自增
            self::query()->where('id', $account)->increment('score', $score);
            return ['code' => 0, 'msg' => '更新成功！'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public static function getUserNum($num)
    {
        try {
            return self::query()->offset($num)->limit(36)->select('id as account', 'nickname', 'score', 'avatar')->get()->toArray();
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}